package com.ruoyi.order.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.order.domain.TableSlave;
import com.ruoyi.order.mapper.TableMainMapper;
import com.ruoyi.order.domain.TableMain;
import com.ruoyi.order.service.ITableMainService;

/**
 * 测试主表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-30
 */
@Service
public class TableMainServiceImpl implements ITableMainService 
{
    @Autowired
    private TableMainMapper tableMainMapper;

    /**
     * 查询测试主表
     * 
     * @param id 测试主表主键
     * @return 测试主表
     */
    @Override
    public TableMain selectTableMainById(Long id)
    {
        return tableMainMapper.selectTableMainById(id);
    }

    /**
     * 查询测试主表列表
     * 
     * @param tableMain 测试主表
     * @return 测试主表
     */
    @Override
    public List<TableMain> selectTableMainList(TableMain tableMain)
    {
        return tableMainMapper.selectTableMainList(tableMain);
    }

    /**
     * 新增测试主表
     * 
     * @param tableMain 测试主表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTableMain(TableMain tableMain)
    {
        int rows = tableMainMapper.insertTableMain(tableMain);
        insertTableSlave(tableMain);
        return rows;
    }

    /**
     * 修改测试主表
     * 
     * @param tableMain 测试主表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTableMain(TableMain tableMain)
    {
        tableMainMapper.deleteTableSlaveByOrderId(tableMain.getId());
        insertTableSlave(tableMain);
        return tableMainMapper.updateTableMain(tableMain);
    }

    /**
     * 批量删除测试主表
     * 
     * @param ids 需要删除的测试主表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTableMainByIds(Long[] ids)
    {
        tableMainMapper.deleteTableSlaveByOrderIds(ids);
        return tableMainMapper.deleteTableMainByIds(ids);
    }

    /**
     * 删除测试主表信息
     * 
     * @param id 测试主表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTableMainById(Long id)
    {
        tableMainMapper.deleteTableSlaveByOrderId(id);
        return tableMainMapper.deleteTableMainById(id);
    }

    /**
     * 新增测试从表信息
     * 
     * @param tableMain 测试主表对象
     */
    public void insertTableSlave(TableMain tableMain)
    {
        List<TableSlave> tableSlaveList = tableMain.getTableSlaveList();
        Long id = tableMain.getId();
        if (StringUtils.isNotNull(tableSlaveList))
        {
            List<TableSlave> list = new ArrayList<TableSlave>();
            for (TableSlave tableSlave : tableSlaveList)
            {
                tableSlave.setOrderId(id);
                list.add(tableSlave);
            }
            if (list.size() > 0)
            {
                tableMainMapper.batchTableSlave(list);
            }
        }
    }
}
